//
//  CTNetHelper.m
//  CT
//
//  Created by liufanwei on 15/4/4.
//  Copyright (c) 2015年 liufanwei. All rights reserved.
//

#import "CTNetHelper.h"
#import "AFNetworking.h"
#import "CommonFunc.h"
#import "AppDelegate.h"
#import "CommonMicro.h"

@interface CTNetHelper()<NSURLConnectionDataDelegate>{
    NSString * URL;
    NSData * _Data;
}


@end

@implementation CTNetHelper

+(instancetype)ShareInstance{
    static CTNetHelper * helper;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[CTNetHelper alloc] init];
        [helper initData];
    });
    return helper;
}

-(void)initData{
    _Data = [[NSData alloc] init];
}

-(void)showProgress{
    //创建UIActivityIndicatorView背底半透明View
    UIView *view = [[UIView alloc] initWithFrame:kAppFrame];
    [view setTag:103];
    [view setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0]];
    [kAppDelegate.window addSubview:view];
    
    UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [activityIndicator setCenter:view.center];
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [activityIndicator startAnimating];
    [view addSubview:activityIndicator];
}

-(void)hidProgress{
    UIView * view = [kAppDelegate.window viewWithTag:103];
    [view removeFromSuperview];
}


- (void)_postWithKey:(NSString*)url callback:(CTNetBlock)callback
{
    [self showProgress];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager POST:url parameters:Nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *result = [[NSString alloc] initWithData:(NSData*)responseObject encoding:NSUTF8StringEncoding];
        NSString * var = [self _deCodeBase64Str:result];
        NSError *error;
        NSData * nsdata = [var dataUsingEncoding:NSUTF8StringEncoding];
        id data = [NSJSONSerialization JSONObjectWithData:nsdata options:kNilOptions error:&error];
        if (callback) {
            callback(data);
        }
        [self hidProgress];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"ERROR:%@", error);
        [self hidProgress];
    }];
}


-(NSString*)_encodeBase64Str:(NSDictionary*)dic{
    NSError * error;
    NSData * data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    NSString *aString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSString * str = [CommonFunc base64StringFromText:aString];
    
    char randomChar[11];
    for (int x=0;x<11;randomChar[x++] = (char)('A' + (arc4random_uniform(26))));
    NSString * randomStr = [[NSString alloc] initWithBytes:randomChar length:11 encoding:NSUTF8StringEncoding];
    
    NSString * url = [randomStr stringByAppendingString:str];
    NSString *outputStr = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                                (CFStringRef)url,
                                                                                                NULL,
                                                                                                (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                                kCFStringEncodingUTF8);
    return outputStr;
}


-(NSString*)_deCodeBase64Str:(NSString*)src{
    NSString * rtn = [NSString string];
    if (!src && src.length <= 11) {
        return rtn;
    }
    rtn = [src substringFromIndex:11];
    rtn = [CommonFunc textFromBase64String:rtn];
    return rtn;
}

-(void)login:(NSDictionary*)dic WithCallBack:(CTNetBlock)blcok{
    NSString * key = [self _encodeBase64Str:dic];
    NSString *url = [NSString stringWithFormat:@"%@new_login.jsp?%@",kBaseUrl,key];
    [self _postWithKey:url callback:blcok];
}

-(void)regiter:(NSDictionary*)dic WithCallBack:(CTNetBlock)blcok{
    NSString * key = [self _encodeBase64Str:dic];
    NSString *url = [NSString stringWithFormat:@"%@new_reg.jsp?%@",kBaseUrl,key];
    [self _postWithKey:url callback:blcok];
}

-(void)remainingMoney:(NSDictionary *)dic WithCallBack:(CTNetBlock)blcok{
    NSString * key = [self _encodeBase64Str:dic];
    NSString *url = [NSString stringWithFormat:@"%@new_querybalance.jsp?%@",kBaseUrl,key];
    [self _postWithKey:url callback:blcok];
}

-(void)makeCall:(NSDictionary*)dic WithCallBack:(CTNetBlock)blcok{
    NSString * key = [self _encodeBase64Str:dic];
    NSString *url = [NSString stringWithFormat:@"%@new_callback.jsp?%@",kBaseUrl,key];
    [self _postWithKey:url callback:blcok];
}

-(void)changePassword:(NSDictionary*)dic WithCallBack:(CTNetBlock)blcok{
    NSString * key = [self _encodeBase64Str:dic];
    NSString *url = [NSString stringWithFormat:@"%@new_setpassword.jsp?%@",kBaseUrl,key];
    [self _postWithKey:url callback:blcok];
}

-(void)passwordVerify:(NSDictionary*)dic WithCallBack:(CTNetBlock)blcok{
    NSString * key = [self _encodeBase64Str:dic];
    NSString *url = [NSString stringWithFormat:@"%@new_sendcode.jsp?%@",kBaseUrl,key];
    [self _postWithKey:url callback:blcok];
}

-(void)resetPassword:(NSDictionary*)dic WithCallBack:(CTNetBlock)blcok{
    NSString * key = [self _encodeBase64Str:dic];
    NSString *url = [NSString stringWithFormat:@"%@new_resetpassword.jsp?%@",kBaseUrl,key];
    [self _postWithKey:url callback:blcok];
}

-(void)charge:(NSDictionary*)dic WithCallBack:(CTNetBlock)blcok{
    NSString * key = [self _encodeBase64Str:dic];
    NSString *url = [NSString stringWithFormat:@"%@new_paycard.jsp?%@",kBaseUrl,key];
    [self _postWithKey:url callback:blcok];
}

-(void)callTime:(NSDictionary*)dic WithCallBack:(CTNetBlock)blcok{
    NSString * key = [self _encodeBase64Str:dic];
    NSString *url = [NSString stringWithFormat:@"%@new_reg.jsp?%@",kBaseUrl,key];
    [self _postWithKey:url callback:blcok];
}
@end



